package com.cnswhy.cloud.server.comsumer.receiver;


import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.mq.ConsumerValueAddMsg;
import com.cnswhy.cloud.server.common.entity.mq.TeamValueAddMsg;
import com.cnswhy.cloud.server.comsumer.event.ConsumerValueAddMessageEvent;
import com.cnswhy.cloud.server.comsumer.event.TeamValueAddMessageEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ConsumerValueAddReceiver {

    @Autowired(required = false)
    private List<ConsumerValueAddMessageEvent> events;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = AmqpExchange.CONSUMER_VALUE_MESSAGE + AmqpExchange.QUEUE),
            exchange = @Exchange(value = AmqpExchange.CONSUMER_VALUE_MESSAGE, type = ExchangeTypes.FANOUT)
    ))
    public void sendMessage(ConsumerValueAddMsg consumerValueAddMsg) {

        if (events != null) {
            for (ConsumerValueAddMessageEvent event : events) {
                try {
                    event.add(consumerValueAddMsg);
                } catch (Exception e) {
                    log.error("注册消息发送失败", e);
                }
            }
        }
    }
}
